<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="author" content="wangbo">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0px;
      padding: 0px;
    }

    table {
      width: 500px;
    }

    tr,
    td,
    th {
      border: 1px solid #ccc;
      border-collapse: collapse;
    }

    /* 选中状态的复选框 */
    /* input:checked{
      width: 200px;height: 200px;
    } */
  </style>
</head>

<body>
  <table cellpadding='0' cellspacing="0">
    <thead>
      <thead>
        <tr>
          <th> <input type="checkbox" name="" id="" onclick="checkAll()"> 全选</th>
          <th>图片</th>
          <th>单价</th>
          <th>数量</th>
          <th onclick="Alldel()">全部删除</th>
        </tr>
      </thead>
    <tbody>
    <tbody>
      <tr>
        <td><input type="checkbox" name="" id=""> </td>
        <td>小米 </td>
        <td>500</td>
        <td>1</td>
        <td onclick="delRow()">删除</td>
      </tr>
      <tr>
        <td><input type="checkbox" name="" id=""> </td>
        <td>小米</td>
        <td>500</td>
        <td>1</td>
        <td onclick="delRow()">删除</td>
      </tr>

      <tr>
        <td><input type="checkbox" name="" id=""> </td>
        <td>小米</td>
        <td>500</td>
        <td>1</td>
        <td onclick="delRow()">删除</td>
      </tr>

      <tr>
        <td><input type="checkbox" name="" id=""> </td>
        <td>小米</td>
        <td>500</td>
        <td>1</td>
        <td onclick="delRow()">删除</td>
      </tr>
    </tbody>
  </table>
  <script>
    // tbody 下边所有的复选框
    let checkArr = document.querySelectorAll("tbody input")
    // 全选按钮
    let check_all = document.querySelector("thead input")
    for (const inputCheck of checkArr) {
      // 给所有的inputcheck 绑定上方法
      inputCheck.onclick = function () {
        let checkArr = document.querySelectorAll("tbody input")
        // tbody下 选中的复选框
        let xuanArr = document.querySelectorAll("tbody input:checked")
        // tbody下选中的复选框 数量 == tbody 下边所有的复选框 说明都选中了
        if (xuanArr.length == checkArr.length) {
          check_all.checked = true
        }else{
          check_all.checked = false
        }
      }
    }

    let checkAll = function () {
      let all = event.target
      // 是选中状态
      for (const inputCheck of checkArr) {
        if (all.checked) {
          inputCheck.checked = true
        } else {
          inputCheck.checked = false
        }
      }
      console.log(all)
    }

    let Alldel = function () {
      let xuanArr = document.querySelectorAll("tbody input:checked")
      for (const input of xuanArr) {
        // console.log(input.parentElement.parentElement)
        // 获取到最近祖先元素,父级元素
        let tr = input.closest("tr")
        tr.remove()        
      }
    }

    let delRow = function () {
      let td = event.target
      // 找父类
      let tr = td.closest("tr")
      // 通过父个元素,获取到最近的子类
      let input =tr.querySelector("input")
      if (input.checked) {
        tr.remove()
      }
    }
  </script>
</body>

</html>